package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import cn.wolfcode.luowowo.website.web.util.UMEditorUploader;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("travel")
public class TravelController {
    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelCommentService travelCommentService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelStatisVOService travelStatisVOService;

    @RequestMapping("")
    public String index(Model model, @ModelAttribute("qo") TravelQuery qo, TravelCommentQuery qo2) {
        model.addAttribute("pageInfo", travelService.query(qo));
        //tcs 根据游记排序查询出前十条评论
        List content = travelCommentService.query(new TravelCommentQuery()).getContent();
        model.addAttribute("tcs", content);
        //热门游记排行前十数据
        List<TravelStatisVO> hotCds = travelStatisVOService.queryHotComments();
        model.addAttribute("hotCds", hotCds);
        return "travel/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        if (id != null) {
            Travel travel = travelService.get(id);
            travel.setTravelContent(travelService.getComment(travel.getId()));
            model.addAttribute("tv", travel);
        }
        //dests
        model.addAttribute("dests", destinationService.getDestsByDeep(3));
        return "travel/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo) {
        travel.setAuthor(userInfo);
        Long id = travelService.saveOrUpdate(travel);
        return AjaxResult.success().addData(id);//这里还要返回一个id保存或者修改的时候
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long id, @UserParam UserInfo userInfo) {
        //热门游记排序.根据评论数和游览数相加分数前十来进行排序显示在压面上//
        travelStatisVOService.addHotScore(id, 1);
        //阅读数量的增加
        travelStatisVOService.increateViewnum(id, 1);
        //根据游记id查询游记的统计对象
        TravelStatisVO vo = travelStatisVOService.getTravelStatisVO(id);
        model.addAttribute("vo", vo);

        //判断当前游记是否被用户收藏,如果收藏就回显
        if (userInfo != null) {//如果用户登录了才查
            Boolean isFavor = travelStatisVOService.isFavor(id, userInfo.getId());
            model.addAttribute("isFavor", isFavor);
        }

        //detail   toasts  sds 攻略明细   ts 游记明细
        Travel travel = travelService.get(id);
        travel.setTravelContent(travelService.getComment(travel.getId()));
        model.addAttribute("detail", travel);

        //toasts
        List<Destination> toasts = destinationService.getToasts(travel.getDest().getId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);

        //sds,查询该目的地下阅读量为前3的攻略明细
        List<StrategyDetail> catalogsViemnumTop3 = strategyDetailService.getCatalogsViemnumTop3(travel.getDest().getId());
        model.addAttribute("sds", catalogsViemnumTop3);

        //ts查询当前关联游记明细,查询该目的地下阅读量为前3的游记明细
        List<Travel> travels = travelService.getTravelsViennumTop3(travel.getDest().getId());
        model.addAttribute("ts", travels);

        //游记的评论
       /* TravelCommentQuery qo = new TravelCommentQuery();
        qo.setTravelId(id);
        qo.setCurrentPage(1);
        qo.setPageSize(Integer.MAX_VALUE);
        Page page = travelCommentService.query(qo);
        //List content = page.getContent();
        model.addAttribute("list", page.getContent());*/
       List<TravelComment> list= travelCommentService.list(id);
       model.addAttribute("list",list);
        return "travel/detail";
       /* 重点是解决游记明细这一块
                里面的字段都是游记的那张表
        但是右边的是关联的攻略
                面包屑,把他爸爸找出来目的地,
                然后查询当前关联攻略明细,查询该目的地下阅读量为前3的攻略明细
        然后查询当前关联游记明细,查询该目的地下阅读量为前3的游记明细*/
    }

    @Value("${file.path}")
    private String filePath;

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, filePath);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(UserInfo userInfo) {
        return userInfo;
    }

    @RequestMapping("/info")
    @ResponseBody
    public Object info(@UserParam UserInfo userInfo) {
        return userInfo;
    }

    //楼中楼的评论操作
    @RequestMapping("/commentAdd")
    public Object commentAdd(Model model, TravelComment travelComment, @UserParam UserInfo userInfo) {
        travelComment.setCity(userInfo.getCity());
        travelComment.setUserId(userInfo.getId());
        travelComment.setHeadUrl(userInfo.getHeadImgUrl());
        travelComment.setLevel(userInfo.getLevel());
        travelComment.setUsername(userInfo.getNickname());
        TravelComment comment = travelCommentService.save(travelComment);
        model.addAttribute("c", comment);
        model.addAttribute("floor", travelCommentService.getCount(travelComment.getTravelId()));
        //旅游明细下的评论数+1
        travelStatisVOService.increateReplynum(travelComment.getTravelId(), 1);
        //热门游记排序.根据评论数和游览数相加分数前十来进行排序显示在压面上//
        travelStatisVOService.addHotScore(travelComment.getTravelId(), 1);
        return "travel/commentTpl";
    }

    //用户收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long tid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult ajaxResult = new AjaxResult(false, "请先登录");
            //当前状态码为102时并且是false表示 没有登录
            ajaxResult.setCode(AjaxResult.NO_LOGIN);
            return ajaxResult;
        }
        //判断用户到底是收藏还是取消收藏
        Boolean ret = travelStatisVOService.favor(tid, userInfo.getId());
        return new AjaxResult(ret, "").addData(travelStatisVOService.getTravelStatisVO(tid));
    }
    //用户分享
    @RequestMapping("/share")
    @ResponseBody
    public Object share(Long tid,@UserParam UserInfo userInfo){
        if (userInfo == null) {
            AjaxResult ajaxResult = new AjaxResult(false, "请先登录");
            return ajaxResult;
        }
        travelStatisVOService.increateSharenum(tid,1);
        return new AjaxResult(true, "").addData(travelStatisVOService.getTravelStatisVO(tid));
    }

    //用户顶
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long tid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult ajaxResult = new AjaxResult(false, "请先登录");
            //当前状态码为102时并且是false表示 没有登录
            ajaxResult.setCode(AjaxResult.NO_LOGIN);
            return ajaxResult;
        }
        //true表示用户尽头还没点赞.false表示用户已经点赞了
        Boolean ret = travelStatisVOService.strategyThumbup(tid, userInfo.getId());
        return new AjaxResult(ret, "").addData(travelStatisVOService.getTravelStatisVO(tid));
    }
}
